package org.goyo.main.services.sale.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.base.IBillNumberDao;
import org.goyo.main.domain.dao.sale.ISaleOrderDao;
import org.goyo.main.domain.entity.sale.SaleOrder;
import org.goyo.main.domain.entity.sale.SaleOrderEntry;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.sale.ISaleOrderService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
@Transactional
public class SaleOrderServiceImpl  extends BaseService  implements ISaleOrderService{
	@Resource
	private ISaleOrderDao saleOrderDao;
	@Resource
	private IBillNumberDao billNumberDao;

	@Override
	public Pager<SaleOrder> findByCondition(String where, Pager pager,Object[] groupUuids)
			throws DBException, ServiceException {
		
		return  saleOrderDao.findByCond(where, pager,groupUuids);
	}

	@Override
	public SaleOrder saveOrUpdateInfo(SaleOrder info, Object[] uuids,SysUser user)
			throws DBException, ServiceException {
		String uuid = null;
		//保存用户数据
		info.setStatus(Constants.SAVESTATUS);
		info.setAlterDate(DateUtils.getCurrentDate());
		info.setAlterUser(user);
		if(info.getUuid() == null || "".equals(info.getUuid())){
			info.setUuid(null);
			info.setCreateDate(DateUtils.getCurrentDate());
			info.setCreateUser(user);
			uuid = saleOrderDao.save(info);
//			alterBillNumber(SaleOrder.BIZTYPE, info.getNumber());
		}else{
			saleOrderDao.update(info);
			uuid = info.getUuid();
		}
		//删除数据
		if(uuids != null && uuids[0] != null){
			saleOrderDao.delBatch(SaleOrderEntry.class, "uuid", uuids);
		}
		info.setUuid(uuid);
		return info;
	}

	@Override
	public List<SaleOrderEntry> findByParentId(String parentId) throws DBException,
			ServiceException {
		return saleOrderDao.findByParentId(parentId);
	}
	
	
	
	@Override
	public void delByUuids(Object[] uuids) throws DBException, ServiceException {
		//批量删除
		for(Object uuid : uuids){
			SaleOrder info = saleOrderDao.findByUuid(SaleOrder.class, uuid.toString());
			saleOrderDao.delete(info);
		}
	}

	@Override
	public SaleOrder submitByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		SaleOrder info = null;
		//批量提交
		for(Object uuid : uuids){
			info = saleOrderDao.findByUuid(SaleOrder.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setSubmitDate(DateUtils.getCurrentDate());
			info.setSubmitUser(user);
			info.setStatus(Constants.SUBMITTATUS);
			saleOrderDao.update(info);
		}
		return info;
	}

	@Override
	public SaleOrder auditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		SaleOrder info = null;
		//批量审核
		for(Object uuid : uuids){
			info = saleOrderDao.findByUuid(SaleOrder.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(DateUtils.getCurrentDate());
			info.setAuditUser(user);
			info.setStatus(Constants.AUDITSTATUS);
			saleOrderDao.update(info);
		}
		return info;
	}

	@Override
	public SaleOrder unAuditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		SaleOrder info = null;
		//批量反审核
		for(Object uuid : uuids){
			info = saleOrderDao.findByUuid(SaleOrder.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(null);
			info.setAuditUser(null);
			info.setSubmitUser(null);
			info.setSubmitDate(null);
			info.setStatus(Constants.SAVESTATUS);
			saleOrderDao.update(info);
		}
		return info;
	}

	@Override
	public SaleOrder findByUuuid(String uuid) throws DBException,
			ServiceException {
		SaleOrder info = saleOrderDao.findByUuid(SaleOrder.class, uuid);
		return info;
	}

	@Override
	public Pager<SaleOrderEntry> findByEntryCond(String where, Pager pager)
			throws DBException, ServiceException {
		pager = saleOrderDao.findByEntryCond(where, pager);
//		List<SaleOrderEntry> entrys = pager.getList();
//		if(entrys != null && entrys.size() > 0){
//			SaleOrder info = saleOrderDao.get(SaleOrder.class, entrys.get(0).getParentId());
//			for(SaleOrderEntry entry :entrys){
//				entry.setSaleOrder(info);
//			}
//		}
		return pager;
	}

	@Override
	public void importExcel(MultipartFile maltipartFile,SysUser user,String billNumber) throws Exception {
//		byte[] bytes = maltipartFile.getBytes();
//		InputStream buffin = new ByteArrayInputStream(bytes, 0, bytes.length);
//		Workbook book = WorkbookFactory.create(buffin);
//		List<SaleOrder> list =  ExcelUtil.excelToObject(book, SaleOrder.class);
//		for(int i=0;i<list.size();i++){
//			if(i > 0)
//				billNumber = ToolUtils.addBillNumber(billNumber);
//			SaleOrder saleOrder = new SaleOrder();
//			saleOrder.setDepGroup(user.getDepGroup());
//			saleOrder.setCreateUser(user);
//			saleOrder.setCreateDate(DateUtils.getCurrentDate());
//			saleOrder.setNumber(billNumber);
//			saleOrder.setStatus(Constants.SAVESTATUS);
//			saleOrderDao.save(saleOrder);
//		}
//		BillNumber billNumberInfo = billNumberDao.get(BillNumber.class, SaleOrder.BIZTYPE);
//		billNumberInfo.setBiztype(SaleOrder.BIZTYPE);
//		billNumberInfo.setNumber(billNumber);
//		billNumberDao.update(billNumberInfo);
	}
}
